/**
 * @format
 * @Author: TC
 * @Date: 2025-09-14 19:36:12
 * @Last Modified by: TC (tengchao@taqu.cn)
 * @Last Modified time: 2025-10-17 16:55:54
 */

import { _decorator, Color, Component, instantiate, Node, Prefab, Sprite, Tween, tween, v3 } from "cc";
import { BasePoolItem } from "db://assets/FrameWork/Module/Component/BasePoolItem";
import { nsMoleEnum } from "../../../Const/MoleEunm";
import { gameMole } from "../../../GameMole";
import { MoleHole } from "../Item/MoleHole";
import { app } from "db://assets/FrameWork/App";
const { ccclass, property } = _decorator;

/**
 * 地鼠
 */
@ccclass("MoleMole")
export class MoleMole extends BasePoolItem {
    @property({ type: Node, displayName: "身体" })
    body: Node = null;
    @property({ type: Node, displayName: "待机脸" })
    faceIdle: Node = null;
    @property({ type: Node, displayName: "受伤脸" })
    faceHit: Node = null;
    @property({ type: Node, displayName: "死亡脸" })
    faceDie: Node = null;
    @property({ type: Prefab, displayName: "帽子预制" })
    pfbCap: Prefab = null;

    @property({ type: Node, displayName: "左眼" })
    nodeEyeLeft: Node = null;
    @property({ type: Node, displayName: "右眼" })
    nodeEyeRight: Node = null;
    private _hp = 1;
    private _hpCap = 1;
    private _idleTime = 3;
    private _status: nsMoleEnum.EMoleStatus = nsMoleEnum.EMoleStatus.IDLE;
    private _canHit = false;
    private _hole: MoleHole = null; //鼠洞
    private _nodeCap: Node = null; //帽子节点

    private static _initMolePos = v3(0, -240, 0);
    private static _idleMolePos = v3(0, -35, 0);

    protected init(): void {
        super.init();
        if (this.nodeEyeLeft) {
            tween(this.nodeEyeLeft).by(1, { angle: 360 }).repeatForever().start();
        }
        if (this.nodeEyeRight) {
            tween(this.nodeEyeRight).by(1, { angle: 360 }).repeatForever().start();
        }
    }

    onEnable(): void {
        super.onEnable();
        this.node.setSiblingIndex(0);
    }

    protected addUIEvent(): void {
        super.addUIEvent();
        this.body.on(Node.EventType.TOUCH_START, this._onTouchBegin, this);
    }

    public onGet(): void {
        // super.onGet();
        // this.node.setSiblingIndex(0);
        this._reset();
        this._takeOnCap();
        this._birth();
    }

    private _reset(): void {
        this._canHit = false;
        this.body.setPosition(MoleMole._initMolePos);
        this._setStatus(nsMoleEnum.EMoleStatus.IDLE);
        this._hp = 1;
        this._hpCap = 1;
    }

    /**
     * 受击
     * @param damage
     */
    private _hit(damage: number): void {
        if (!this._canHit) return;
        if (this._hpCap > damage) {
            this._hpCap -= damage;
            damage = 0;
        } else {
            damage -= this._hpCap;
            this._hpCap = 0;
            //to do 帽子掉落
            this._takeOffCap();
        }

        this._hp -= damage;
        if (this._hp > 0) {
            this._hurt();
        } else {
            this._die();
        }
    }

    /**
     * 出生
     */
    private _birth(): void {
        this._idleTime = 100; //gameMole.battleMgr.missionCfg.idleTime || 3;
        this._canHit = true;
        this.body.setScale(v3(1, 1, 1));
        tween(this.body)
            .to(0.3, { position: MoleMole._idleMolePos })
            .to(0.1, { scale: v3(1, 1.1, 1) })
            .to(0.1, { scale: v3(1, 1, 1) })
            .delay(this._idleTime)
            .call(() => {
                this._setStatus(nsMoleEnum.EMoleStatus.IDLE);
                this._back(false);
            })
            .start();
    }

    /**
     * 受伤
     */
    private _hurt(): void {
        this._setStatus(nsMoleEnum.EMoleStatus.HIT);
        this.body.setScale(v3(1, 1, 1));
        tween(this.body).stop();
        tween(this.body)
            .to(0.1, { scale: v3(1.2, 0.8, 1) })
            .to(0.1, { scale: v3(1, 1, 1) })
            .call(() => {
                this._setStatus(nsMoleEnum.EMoleStatus.IDLE);
            })
            .start();
    }

    /**
     * 死亡
     */
    private _die(): void {
        this._canHit = false;

        this._setStatus(nsMoleEnum.EMoleStatus.DIE);
        this._back();
        //   this.dieHandle();
        //   gameDataMgr.kill++;
        gameMole.battleMgr.killMole();
    }

    /**
     * 返回
     * @param scale 是否播放缩放动画
     */
    private _back(scale: boolean = true): void {
        this._canHit = false;
        this.body.setScale(v3(1, 1, 1));
        tween(this.body).stop();
        const scaleTime = scale ? 0.15 : 0;
        tween(this.body)
            .to(scaleTime, { scale: v3(1.2, 0.8, 1) })
            .to(scaleTime, { scale: v3(1, 1, 1) })
            .to(0.3, { position: MoleMole._initMolePos })
            .call(() => {
                this.body.getComponent(Sprite).color = Color.WHITE;
                if (this._hole) {
                    this._hole.reset();
                }
                this.putToPool();
            })
            .start();
        this._handleBackResult();
    }

    /**
     * 戴帽子
     */
    private _takeOnCap() {
        if (!this._nodeCap) {
            this._nodeCap = gameMole.capMgr.getCap();
            this._nodeCap.parent = this.body;
            this._nodeCap.setPosition(v3(0, 0, 0));
        }
    }

    /**
     * 取下帽子
     */
    private _takeOffCap() {
        if (this._nodeCap) {
            const nodeCap = this._nodeCap;
            gameMole.capMgr.playCapAni(nodeCap);
            this._nodeCap = null;
        }
    }

    /**
     * 设置状态
     */
    private _setStatus(status: nsMoleEnum.EMoleStatus): void {
        this._status = status;
        this._updateFace();
    }

    /**
     * 刷新面部表情
     */
    private _updateFace(): void {
        this.faceIdle.active = this._status == nsMoleEnum.EMoleStatus.IDLE;
        this.faceHit.active = this._status == nsMoleEnum.EMoleStatus.HIT;
        this.faceDie.active = this._status == nsMoleEnum.EMoleStatus.DIE;
    }

    /**
     * 点击开始
     */
    private _onTouchBegin(): void {
        this._hit(gameMole.dataMgr.attack);
    }

    public setHole(hole: MoleHole): void {
        this._hole = hole;
    }

    /**
     * 处理返回结果
     * @returns
     */
    private _handleBackResult(): void {
        if (this._status == nsMoleEnum.EMoleStatus.DIE) {
            gameMole.battleMgr.killMole();
            return;
        } else {
            gameMole.battleMgr.moleRunAway();
        }
    }

    /**
     * 暂停地鼠
     */
    public pause(): void {
        Tween.pauseAllByTarget(this.body);
        Tween.pauseAllByTarget(this.nodeEyeLeft);
        Tween.pauseAllByTarget(this.nodeEyeRight);
    }

    /**
     * 继续地鼠
     */
    public resume(): void {
        Tween.resumeAllByTarget(this.body);
        Tween.resumeAllByTarget(this.nodeEyeLeft);
        Tween.resumeAllByTarget(this.nodeEyeRight);
    }

    /*************对象池相关接口*************** */
    /**
     * 被放入对象池时的回调
     */
    onPut(): void {
        Tween.stopAllByTarget(this.body);
        if (this._nodeCap) {
            this._nodeCap.destroy();
            this._nodeCap = null;
        }
        if (this._hole) {
            this._hole = null;
        }
    }

    /**
     * 释放
     */
    public release(): void {
        // this._reset();
        gameMole.capMgr.removeCap(this._nodeCap);
        this._nodeCap = null;
        this.putToPool();
    }
}
